/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Panel;

import DAO.*;
import Gui.MainForm;
import OBJECT.DangKiVanChuyen;
import OBJECT.LichTrinh;
import OBJECT.TuyenDuong;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Ngoc Anh
 */
public class TimKiemLichTrinh extends javax.swing.JPanel {

    /**
     * Creates new form TimKiemLichTrinh
     */
    LichTrinh lt;
    public MainForm main;
   DefaultTableModel tablemodelLichTrinh;
   DefaultTableModel tablemodelVanChuyen;
   
   private List<TuyenDuong> DanhSachTuyenDuong = null;
   LichTrinhDAO ltdao;
   ChiTietTuyenDuongDAO cttddao;
   DefaultComboBoxModel combox;
   DangKiVanChuyenDao dkvc ;
   QuyDinhDAO qd;
  private String username;
   int MaKH ;
  private String password;
 private String port;
  private String DataBase;
   
  private String Host;
   DangKiVanChuyen dk ;
   TuyenDuongDAO tddao;
   List<TuyenDuong> listtd ;
   boolean chon2 = false;
   boolean chon1 = false;
   int Matd;
   static int malt;
   public MainForm mainform;
    public TimKiemLichTrinh(String User ,String Pass,String port,String DataBase,String Host,MainForm main) {
        initComponents();
        jButtondangki.addActionListener(main);
        mainform=main;
         tbLichTrinh.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
          tablemodelLichTrinh = new DefaultTableModel();
          combox = new DefaultComboBoxModel();
          this.DataBase=DataBase;
          this.Host=Host;
          this.username=username;
          
          this.password=password;
          this.port=port;
        tablemodelLichTrinh.addColumn("Mã Lt");
        tablemodelLichTrinh.addColumn("Mã Tuyến Đường");
        tablemodelLichTrinh.addColumn("Mã Tàu");
        tablemodelLichTrinh.addColumn("Thời Gian Khởi Hành");
        tablemodelLichTrinh.addColumn("Thời Gian Đến");
        tablemodelLichTrinh.addColumn("Số Contenter Còn Trống");
        tablemodelLichTrinh.addColumn("Tình Trạng");
        tbLichTrinh.setModel(tablemodelLichTrinh);
        
        tddao = new TuyenDuongDAO(User, Pass, port, DataBase, Host);
        ltdao = new LichTrinhDAO(User, Pass, port, DataBase, Host);
        cttddao=new ChiTietTuyenDuongDAO(User, Pass, port, DataBase, Host);
        listtd = new Vector<TuyenDuong> ();
        showTuyenDuong();
       
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jRadioButtonTuyenDuong = new javax.swing.JRadioButton();
        jRadioButtonTenGga = new javax.swing.JRadioButton();
        jTextField1 = new javax.swing.JTextField();
        jButtonTimKiem = new javax.swing.JButton();
        jComboBoxTuyenDuong = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbLichTrinh = new javax.swing.JTable();
        jButtondangki = new javax.swing.JButton();
        jComboBoxGaDen = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Tim Kiem Lich Trinh ", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Adobe Caslon Pro", 1, 14), java.awt.Color.orange)); // NOI18N

        buttonGroup1.add(jRadioButtonTuyenDuong);
        jRadioButtonTuyenDuong.setText("Tuyen duong");
        jRadioButtonTuyenDuong.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jRadioButtonTuyenDuongMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jRadioButtonTuyenDuongMousePressed(evt);
            }
        });

        buttonGroup1.add(jRadioButtonTenGga);
        jRadioButtonTenGga.setText("Ten Ga");
        jRadioButtonTenGga.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jRadioButtonTenGgaMouseClicked(evt);
            }
        });

        jTextField1.setText("jTextField1");

        jButtonTimKiem.setText("Tim Kiem");
        jButtonTimKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonTimKiemActionPerformed(evt);
            }
        });

        jComboBoxTuyenDuong.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBoxTuyenDuong.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jComboBoxTuyenDuongMouseClicked(evt);
            }
        });
        jComboBoxTuyenDuong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxTuyenDuongActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jRadioButtonTenGga)
                                .addGap(48, 48, 48)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jRadioButtonTuyenDuong)
                                .addGap(18, 18, 18)
                                .addComponent(jComboBoxTuyenDuong, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(91, 91, 91)
                        .addComponent(jButtonTimKiem)))
                .addContainerGap(103, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jRadioButtonTenGga))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButtonTuyenDuong)
                    .addComponent(jComboBoxTuyenDuong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addComponent(jButtonTimKiem)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBackground(new java.awt.Color(0, 153, 153));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Danh Sach Lich Trinh Van Chuyen", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Bodoni MT", 1, 18), java.awt.Color.orange)); // NOI18N
        jPanel3.setForeground(new java.awt.Color(204, 51, 0));

        tbLichTrinh.setBackground(new java.awt.Color(204, 204, 204));
        tbLichTrinh.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        tbLichTrinh.setForeground(new java.awt.Color(0, 153, 153));
        tbLichTrinh.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbLichTrinh.setCellSelectionEnabled(true);
        tbLichTrinh.setSelectionBackground(new java.awt.Color(255, 255, 255));
        tbLichTrinh.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbLichTrinhMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbLichTrinhMousePressed(evt);
            }
        });
        jScrollPane2.setViewportView(tbLichTrinh);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 631, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );

        jButtondangki.setText("Dang ki");

        jComboBoxGaDen.setBackground(new java.awt.Color(0, 153, 153));
        jComboBoxGaDen.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jComboBoxGaDen.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jComboBoxGaDenMouseClicked(evt);
            }
        });
        jComboBoxGaDen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxGaDenActionPerformed(evt);
            }
        });

        jLabel1.setText("Danh sach cac ga den");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButtondangki)
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                            .addComponent(jComboBoxGaDen, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(24, 24, 24))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jComboBoxGaDen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32)
                .addComponent(jButtondangki)
                .addContainerGap(37, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
 
    private void showList(int ma)
        {
                        String Soluong="";
                        CallableStatement cls=ltdao.TimLichTrinhTheoTD(ma);
                        try
                        {          
                            cls.execute();
                            ResultSet result = cls.getResultSet(); 
                            while(result.next())
                            {
                                if(result.getString("TinhTrang").equals("2")==false)
                                {
                                    Vector vt = new Vector();
                                    vt.add(result.getInt("Ma_LichTrinhVC"));
                                    vt.add(result.getInt("Ma_TuyenDuong"));
                                    vt.add(result.getInt("Ma_Tau"));
                                    Date d=result.getDate("ThoiGianKhoiHanh");
                                    vt.add(d.getDate()+"-"+(int)(d.getMonth()+1)+"-"+(int)(d.getYear()+1900));
                                    Date d1=result.getDate("ThoiGianDen");
                                    vt.add(d1.getDate()+"-"+(int)(d.getMonth()+1)+"-"+(int)(d.getYear()+1900));
                                    vt.add(result.getInt("SoContainerConTrong"));
                                    vt.add(result.getString("TinhTrang"));
                                    tablemodelLichTrinh.addRow(vt);
                                    
                                }
                            }
                            result.close();                           
                            cls.close();
                            ltdao.CloseConnect();
                        }catch(Exception e)
                        {
                            ;
                        }

        }
    private void tbLichTrinhMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbLichTrinhMouseClicked
        // TODO add your handling code here:
        
        int row = this.tbLichTrinh.getSelectedRow();
        
        int giatri = Integer.parseInt(tbLichTrinh.getValueAt(row, 1).toString());
         int malt = Integer.parseInt(tbLichTrinh.getValueAt(row, 0).toString());
        lt=ltdao.Search1LichTrinh(malt);
        mainform.lt=lt;
        System.out.println(malt);
        CallableStatement listcttd = cttddao.DanhSachGaCua1TuyenDuong(giatri);
//       combox = (DefaultComboBoxModel)jComboBoxGaDen.getModel();
//        combox.removeAllElements();
        try {
            listcttd.execute();
            ResultSet result = listcttd.getResultSet();

            while (result.next()) {
                Vector vt = new Vector();
                vt.add(result.getInt("MaCT_TuyenDuong"));
                vt.add(result.getString("TenGaDen"));
            }
            result.close();
            listcttd.close();
            cttddao.CloseConnect();
        } catch (Exception exc) {
            System.out.println(exc);
        }

    }//GEN-LAST:event_tbLichTrinhMouseClicked

    private void tbLichTrinhMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbLichTrinhMousePressed
//        // TODO add your handling code here:

   }//GEN-LAST:event_tbLichTrinhMousePressed

    
    private void jRadioButtonTuyenDuongMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jRadioButtonTuyenDuongMouseClicked
        // TODO add your handling code here:
        
        
    }//GEN-LAST:event_jRadioButtonTuyenDuongMouseClicked

    private void jRadioButtonTenGgaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jRadioButtonTenGgaMouseClicked
        // TODO add your handling code here:
        chon1 = true;
        chon2 = false;
    }//GEN-LAST:event_jRadioButtonTenGgaMouseClicked
     private void clearListLichTrinh()
        {
                 for(int i = tablemodelLichTrinh.getRowCount() -1;i >= 0;i--)
                     tablemodelLichTrinh.removeRow(i);   
        }
    private void showTuyenDuong()
    {
        combox = (DefaultComboBoxModel)jComboBoxTuyenDuong.getModel();
        combox.removeAllElements();
      
        if(chon2 == true) // tim kiem theo tuyen duong
        {
            CallableStatement td = tddao.DanhSachTuyenDuong();
             try
            {                
                td.execute(); 
                ResultSet result=td.getResultSet(); 
                int i = 0;
                 while(result.next())
                 {  
                     TuyenDuong tdd = new TuyenDuong();                 
                     tdd.setTenTuyenDuong(result.getString("Ten_TuyenDuong"));
                     tdd.setMaTuyenDuong(result.getInt("Ma_TuyenDuong"));
                     listtd.add(tdd);
                     jComboBoxTuyenDuong.addItem(listtd.get(i).getTenTuyenDuong());                     
                     i++;                     
                 }                 
                 result.close();  
                 td.close();
                 tddao.CloseConnect();
                 Matd = listtd.get(0).getMaTuyenDuong(); // Khoi tao matd bang dong dau tien trong combobox
            }catch(Exception exc)
            {
                 System.out.println("bucboi");
            }             
        }        

    }
    private void jButtonTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonTimKiemActionPerformed
        // TODO add your handling code here:
       clearListLichTrinh();
       showList(Matd);
    }//GEN-LAST:event_jButtonTimKiemActionPerformed

    private void jComboBoxTuyenDuongMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jComboBoxTuyenDuongMouseClicked
        // TODO add your handling code here:

      int i = this.jComboBoxTuyenDuong.getSelectedIndex();    
       Matd = listtd.get(i).getMaTuyenDuong();

    }//GEN-LAST:event_jComboBoxTuyenDuongMouseClicked

    private void jComboBoxTuyenDuongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxTuyenDuongActionPerformed
        // TODO add your handling code here:

    }//GEN-LAST:event_jComboBoxTuyenDuongActionPerformed

    private void jRadioButtonTuyenDuongMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jRadioButtonTuyenDuongMousePressed
        // TODO add your handling code here:
        chon2 = true;
        chon1 = false;
        showTuyenDuong();
       
    }//GEN-LAST:event_jRadioButtonTuyenDuongMousePressed

    private void jComboBoxGaDenMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jComboBoxGaDenMouseClicked
        // TODO add your handling code here:

   }//GEN-LAST:event_jComboBoxGaDenMouseClicked

    private void jComboBoxGaDenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxGaDenActionPerformed
        // TODO add your handling code here:
        //    jTextGaDen.setText(this.jComboBoxGaDen.getSelectedItem().toString());
    }//GEN-LAST:event_jComboBoxGaDenActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButtonTimKiem;
    private javax.swing.JButton jButtondangki;
    private javax.swing.JComboBox jComboBoxGaDen;
    private javax.swing.JComboBox jComboBoxTuyenDuong;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JRadioButton jRadioButtonTenGga;
    private javax.swing.JRadioButton jRadioButtonTuyenDuong;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTable tbLichTrinh;
    // End of variables declaration//GEN-END:variables
}
